package com.example.kotlintest.chat.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.kotlintest.R;
import com.example.kotlintest.chat.ChatActivity;
import com.example.kotlintest.chat.EmojiUtils;
import com.example.kotlintest.chat.PhotoPicker;
import com.guoxiaoxing.phoenix.core.listener.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linzi
 * @date 2022-9-30
 */
public class ChatUIManager {
    private static final String TAG = "ChatUIManager";
    private View leftIcon;

    private View btVoice;
    private EditText edMsg;

    private View ivMask;
    private View ivAdd;
    private RecyclerView rcMenu;

    private View tvSend;

    private View menuLine;

    private boolean isAdd=false;

    private Context context;

    public ChatUIManager(Context context, View leftIcon, View btVoice, EditText edMsg, View ivMask, View ivAdd, RecyclerView rcMenu, View tvSend) {
        this.context=context;
        this.leftIcon = leftIcon;
        this.btVoice = btVoice;
        this.edMsg = edMsg;
        this.ivMask = ivMask;
        this.ivAdd = ivAdd;
        this.rcMenu = rcMenu;
        this.tvSend = tvSend;
    }
    public void setMenuLine(View view){
        menuLine=view;
    }

    public void ivAdd(){
        if(rcMenu.getVisibility()==View.GONE||!isAdd){
            //设置为添加菜单打开状态
            isAdd=true;
            //隐藏输入法
            hideInputMethod(edMsg);
            //让底部添加菜单的recycleview显示出来
            rcMenu.setVisibility(View.VISIBLE);
            //底部添加菜单分割线显示
            if(menuLine!=null) {
                menuLine.setVisibility(View.VISIBLE);
            }
            //按住说话按钮隐藏
            btVoice.setVisibility(View.GONE);
            //输入框显示
            edMsg.setVisibility(View.VISIBLE);
            //恢复左侧语音或者输入按钮图标的显示为默认
            leftIcon.setBackground(context.getDrawable(R.drawable.icon_voice));
            //加载底部菜单适配器
            BottomMenuAdapter.init(context, rcMenu, new BottomMenuAdapter.OnItemClickListener() {
                @Override
                public void itemClick(BottomMenuAdapter.MenuHolder menuHolder, int postion) {
                    Toast.makeText(context,"点击了菜单："+postion,Toast.LENGTH_SHORT).show();
                    switch (postion){
                        case 0:
                            PhotoPicker.init(new ImageLoader() {
                                @Override
                                public void loadImage(Context context, ImageView imageView, String s, int i) {
                                    Glide.with(context)
                                            .load(s)
                                            .into(imageView);
                                }
                            });
                            PhotoPicker photoPicker=new PhotoPicker();
                            photoPicker.setMaxPickNumber(1);
                            photoPicker.setCallback(new PhotoPicker.photoPickerBack() {
                                @Override
                                public void getFinalPath(@NonNull List<String> arrayList, @NonNull List<String> arrayList1, @NonNull List<String> arrayList2) {
                                    if(arrayList.size()>0) {
                                        SpannableString ss= EmojiUtils.getFileMask(context, arrayList.get(0));
                                        if(edMsg.getText().length()==0){
                                            edMsg.append(ss);
                                        }else{
                                            int start=edMsg.getSelectionStart();
                                            int end=edMsg.getSelectionEnd();
                                            edMsg.getText().replace(Math.min(start,end),Math.max(start,end),ss,0,ss.length());
                                        }
//                                            edMsg.setSelection(edMsg.getText().length());
                                    }
                                }
                            });
                            PhotoPicker.show(context,0,new ArrayList<String>());
                            break;
                    }
                }
            });
            //让消息记录recycleview滑动到底部
//            toBottom();
        }else{
            //显示输入法，有可能不生效
            showInputMethod(edMsg);
        }
    }

    public void leftIcon(){
        //如果当前是编辑状态则显示语音按钮
        if(btVoice.getVisibility()==View.GONE){
            //隐藏输入法
            hideInputMethod(edMsg);
            //按住说话按钮显示
            btVoice.setVisibility(View.VISIBLE);
            //消息编辑框隐藏
            edMsg.setVisibility(View.GONE);
            //底部添加菜单的recycleview隐藏
            rcMenu.setVisibility(View.GONE);
            //分割线隐藏
            if(menuLine!=null) {
                menuLine.setVisibility(View.GONE);
            }
            //左侧编辑或者语音按钮图标变为键盘图标
            leftIcon.setBackground(context.getDrawable(R.drawable.icon_key));
            //右侧添加图标显示
            ivAdd.setVisibility(View.VISIBLE);
            //消息发送按钮隐藏
            tvSend.setVisibility(View.GONE);
            //滑动到底部
//            toBottom();
        }else{
            //按住说话按钮隐藏
            btVoice.setVisibility(View.GONE);
            //消息编辑框显示
            edMsg.setVisibility(View.VISIBLE);
            //左侧编辑或者语音按钮图标变为语音图标
            leftIcon.setBackground(context.getDrawable(R.drawable.icon_voice));
            //如果消息编辑框有信息，则把添加图标隐藏，发送按钮显示
            if(edMsg.getText().length()>0) {
                ivAdd.setVisibility(View.GONE);
                tvSend.setVisibility(View.VISIBLE);
            }
            //显示输入法
            showInputMethod(edMsg);
        }
    }


    public void ivMask(){
        //如果底部菜单没被打开，或者当前已经显示菜单，就加载表情适配器
        if(rcMenu.getVisibility()==View.GONE||isAdd){
            //设置点击添加菜单状态为false
            isAdd=false;
            //隐藏输入法
            hideInputMethod(edMsg);
            //显示底部菜单栏的recycleview
            rcMenu.setVisibility(View.VISIBLE);
            //显示分割线
            if(menuLine!=null) {
                menuLine.setVisibility(View.VISIBLE);
            }
            //按住说话按钮隐藏
            btVoice.setVisibility(View.GONE);
            //消息编辑框显示
            edMsg.setVisibility(View.VISIBLE);
            //左侧编辑或者语音按钮图标变为语音图标
            leftIcon.setBackground(context.getDrawable(R.drawable.icon_voice));
            MaskAdapter.init(rcMenu, context, new MaskAdapter.OnItemClickListener() {
                @Override
                public void itemClick(MaskAdapter.MaskHodler menuHolder, int postion) {
                    SpannableString ss=EmojiUtils.getMask(context,postion);
                    if(edMsg.getText().length()==0){
                        edMsg.append(ss);
                    }else{
                        int start=edMsg.getSelectionStart();
                        int end=edMsg.getSelectionEnd();
                        edMsg.getText().replace(Math.min(start,end),Math.max(start,end),ss,0,ss.length());
                    }
                    edMsg.requestFocus();
                }
            });
//            toBottom();
        }else{
            showInputMethod(edMsg);
        }
    }

    public void keyboardShow(){
        isAdd=false;
        if(rcMenu.getVisibility()==View.VISIBLE){
            rcMenu.setVisibility(View.GONE);
            if(menuLine!=null) {
                menuLine.setVisibility(View.GONE);
            }
            leftIcon.setBackground(context.getDrawable(R.drawable.icon_voice));
        }
    }

    //显示输入法
    private void showInputMethod(View view){
        Log.e(TAG, "showInputMethod: " );
        view.setFocusable(true);
        view.requestFocus();
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
    }
    //隐藏输入法
    private void hideInputMethod(View view){
        try {
            ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(view.getWindowToken(), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
